package com.common.util;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import sun.misc.BASE64Encoder;

import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.HashMap;
import java.util.Map;

public class RsaUtil {

    /**
     * 获取公钥的key
     */
    private static final String PUBLIC_KEY = "RSAPublicKey";

    /**
     * 获取私钥的key
     */
    private static final String PRIVATE_KEY = "RSAPrivateKey";

    /**
     * 生成公私钥
     * @return
     */
    public static Map<String,String> generateKeyPair(){
        KeyPair rsa = SecureUtil.generateKeyPair("RSA");
        //公钥加密
        PublicKey publicKey = rsa.getPublic();
        //私钥解密
        PrivateKey privateKey = rsa.getPrivate();
        // 获取 公钥和私钥 的 编码格式（通过该 编码格式 可以反过来 生成公钥和私钥对象）
        byte[] pubEncBytes = publicKey.getEncoded();
        byte[] priEncBytes = privateKey.getEncoded();

        // 把 公钥和私钥 的 编码格式 转换为 Base64文本 方便保存
        String pubEncBase64 = new BASE64Encoder().encode(pubEncBytes);
        String priEncBase64 = new BASE64Encoder().encode(priEncBytes);

        Map<String, String> map = new HashMap<String, String>(2);
        map.put(PUBLIC_KEY,pubEncBase64);
        map.put(PRIVATE_KEY,priEncBase64);
        return map;
    }


    /**
     *
     * @param publicKey 公钥
     * @param content 需要加密的字符串
     * @return
     */
    public static String publicKeyEncrypt(String publicKey,String content){
        //单独传入公钥加密
        RSA rsa = new RSA(null, publicKey);
        return rsa.encryptBase64(content, KeyType.PublicKey);
    }

    /**
     *
     * @param privateKey 私钥
     * @param content 被公钥加密后的字符串
     * @return
     */
    public static String privateKeyDecrypt(String privateKey,String content){
        //单独传入公钥加密
        RSA rsa = new RSA(privateKey, null);
        return rsa.decryptStr(content,KeyType.PrivateKey);
    }


    public static void main(String[] args) {
        Map<String, String> stringStringMap = generateKeyPair();
        System.out.println(stringStringMap);

        String s = publicKeyEncrypt("MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCM+HKHOKkmsX3HQpovMYkiG3efQIvMODJrV00M\n" +
                "skSJ2j1nT7y/yzj3AMwlpNQDuPbrrEuBtMsMfjnt28FWrAXQuaeA5X/9jktyX8d+PXyOrQu1da9V\n" +
                "lLd/qWDoMNjaXJH8jlF23p8eU8S1TFywHKOI71XJQvASwd1FHSSetwIoHwIDAQAB", "1111");

        String s1 = privateKeyDecrypt("MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIz4coc4qSaxfcdCmi8xiSIbd59A\n" +
                "i8w4MmtXTQyyRInaPWdPvL/LOPcAzCWk1AO49uusS4G0ywx+Oe3bwVasBdC5p4Dlf/2OS3Jfx349\n" +
                "fI6tC7V1r1WUt3+pYOgw2NpckfyOUXbenx5TxLVMXLAco4jvVclC8BLB3UUdJJ63AigfAgMBAAEC\n" +
                "gYBKQlo6Zxub2WXTgH8uGx8NC4ERL/Q0iOnuZ5eb95BpOcCc2oEoW0pPu2jO76wLpfTUovdHhVBs\n" +
                "7VVguiF3obiRuenx7U+6B2PY6Xl+38VpObt5V8/4sIT6PYtecSaQwTGXvSFhvCUgq1D8PPn5px7W\n" +
                "xrPFyV4btUKf4FSvlotJGQJBAMRZTy9zaAompD+ychuP7rsIxjCWNChzKCC8bcnLC+QlTVQO/SEw\n" +
                "/AK5g/iaJx6O/dxDjQ12sz0wQfmhkuCHH0UCQQC3zC6d9ZJbO/YIRqN+GjT6WTn6NzX9THxawHa4\n" +
                "fyLOks6pzOf+rcnsH5h5lYEld4gPUomWRHTmwZLx4l0bud4TAkEAkXDaL/rbaRGAftJOpc3y90Yj\n" +
                "kF/imMfcRItvA5fM4h0jb41+wTRIqDgE4oRFT+R5dMvw0rVHRZ7Wb/PYGUrdxQJALb/kRqtOz+jf\n" +
                "9/VlUslBMKOvG8pMZQ150rkflBMJXfQK/xcjFDC8F0tSD8mYCPR06KN53e6STrIWWqEm3fTpAwJA\n" +
                "FMhF4/cv9e3VOjbAJPoeJHkcF8CfppYzvwzbZCrfOutjJ+XnR9pPWmVR4AIJbvUF63dRVI/VHbUw\n" +
                "PFypB7QoQg==", s);

        System.out.println("加密后: "+s);
        System.out.println("解密后: "+s1);
    }
}
